/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.Arkham;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Equiping extends Activity  implements OnClickListener{
	
	TextView name, price;
    Item item = new Item();
    String[] players;
    String[] location;
    int[] locationIndex;
    int selection;
    int equipHere;
    String Where;
    int Who;
	int equipLoc = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
	    Intent i = getIntent();
	    item = (Item) i.getParcelableExtra("item");
	    item.setModifiers((Stats) i.getParcelableExtra("itemStats"));
	    players = i.getStringArrayExtra("players");
	    location = i.getStringArrayExtra("locations");
	    locationIndex = i.getIntArrayExtra("locationIndexes");
	    selection = i.getIntExtra("selection", -1);
		
		//removes title bar and sets the theme
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        
		super.onCreate(savedInstanceState);
		setContentView(R.layout.equipitem);
		
		name = (TextView) findViewById(R.id.itemName);
		name.setText(item.getName());
		price = (TextView) findViewById(R.id.purchasePrice);
		price.setText("Price: $" + item.getPurchasePrice() + "\n");
		
	    GridView gridviewStats = (GridView) findViewById(R.id.itemDetailGrid);
	    gridviewStats.setAdapter(new ItemAdapter(this));
	    
	    ((TextView) findViewById(R.id.middletext)).setText(" with " + item.getName() + " on ");
	    
        View cancelButton = findViewById(com.Arkham.R.id.cancel_button);
        cancelButton.setOnClickListener(this); 
        View equipButton = findViewById(com.Arkham.R.id.equip_button);
        equipButton.setOnClickListener(this); 
        
	    Spinner playerSpinner = (Spinner) findViewById(R.id.player_spinner);
	    ArrayAdapter<String> playerAdapter = new ArrayAdapter<String>(
	            this, android.R.layout.simple_spinner_item, players);
	    playerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    playerSpinner.setAdapter(playerAdapter);
	    
	    Spinner locationSpinner = (Spinner) findViewById(R.id.location_spinner);
	    ArrayAdapter<String> locationAdapter = new ArrayAdapter<String>(
	            this, android.R.layout.simple_spinner_item, location);
	    locationAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    locationSpinner.setAdapter(locationAdapter);

	    playerSpinner.setOnItemSelectedListener(new MyOnPlayerSelectedListener());
	    locationSpinner.setOnItemSelectedListener(new MyOnLocationSelectedListener());
	}
	
	public void onClick(View v) {

		switch (v.getId())
    	{
    	case com.Arkham.R.id.cancel_button:
    	    finish();
    	    break;
    	case com.Arkham.R.id.equip_button:
/*    		if(location[equipHere] == "Head")
    			equipLoc = Constants.HEAD;
    		else if(Where == "Body")
    			equipLoc = Constants.BODY;
    		else if(Where == "Arms")
    			equipLoc = Constants.ARMS;
    		else if(Where == "Right Hand")
    			equipLoc = Constants.R_HAND;
    		else if(Where == "Left Hand")
    			equipLoc = Constants.L_HAND;
    		else if(Where == "Feet")
    			equipLoc = Constants.FEET;
    		else if(Where == "Accessory")
    		 	equipLoc = Constants.ACC;
*/
    		//Step 6, EQUIP
    		GameFile.party.equip(selection, equipLoc, GameFile.party.characters[Who]);
    		String write;
    		write = "Attempting to equip " + GameFile.party.inventory.get(selection).getName() + "(" + selection + ")" +
    		        " to location " + Where + "(" + equipLoc + ")" +
    		        " to " + GameFile.party.characters[Who].getName();
    		Log.i("Equip Log", write);

    		finish();
    	    break;
    	default:
    		finish();
    	}
	}
	
	public class MyOnPlayerSelectedListener implements OnItemSelectedListener {

	   public void onItemSelected(AdapterView<?> parent,
		        View view, int pos, long id) {

		    Who = pos;
		      
		    }

		    public void onNothingSelected(AdapterView parent) {
		      // Do nothing.
		    }
		}	
	
	public class MyOnLocationSelectedListener implements OnItemSelectedListener {

		   public void onItemSelected(AdapterView<?> parent,
			        View view, int pos, long id) {

			    Where = parent.getItemAtPosition(pos).toString();
//			    Where = location[pos];
			    equipLoc = locationIndex[pos];
			      
			    }

			    public void onNothingSelected(AdapterView parent) {
			      // Do nothing.
			    }
			}
		
	public class ItemAdapter extends BaseAdapter {
		private Context context;
		private String[] texts = {
		 "Strength: ", "+" + Integer.toString(item.getStrengthModifier()), 
		 "Attack: ", "+" + Integer.toString(item.getAttackPowerModifier()),
		 "Agility: ", "+" + Integer.toString(item.getAgilityModifier()),
		 "Hit%: ", "+" + Integer.toString(item.getHitPercentageModifier()),
		 "Vitality: ", "+" + Integer.toString(item.getVitalityModifier()),
		 "Defense: ", "+" + Integer.toString(item.getDefenseModifier()),
		 "Magic: ", "+" + Integer.toString(item.getMagicModifier()),
		 "Evade: ", "+" + Integer.toString(item.getEvadeModifier()),
		 "Magic Defense: ", "+" + Integer.toString(item.getMagicDefenseModifier()),
		 "Magic Evade: ", "+" + Integer.toString(item.getMagicEvadeModifier())
		 };
		
		
		public ItemAdapter(Context context) {
		    this.context = context;
		}

		public int getCount() {
		    return texts.length;
		}

		public Object getItem(int position) {
		    return null;
		}

		public long getItemId(int position) {
		    return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
		    TextView tv;
		    if (convertView == null) {
		        tv = new TextView(context);
		        tv.setLayoutParams(new GridView.LayoutParams(85, 50));
		    }
		    else {
		        tv = (TextView) convertView;
		    }
		    tv.setText(texts[position]);
		    return tv;
		}

	}
}